package com.wb.utils

import org.apache.commons.io.FileUtils
import java.io.File
import java.io.InputStream
import java.net.HttpURLConnection
import java.net.URL

/**
 * 网络文件工具
 *
 * @author 祁雪
 */
class FileNetUtil {

    companion object {

        fun downHttpUrl(
            url: String,
            dir: String,
            fileName: String
        ) {
            println("下载链接: ${url}")

            val httpUrl = URL(url)

            if ("https".equals(httpUrl.protocol, true)) {
                SSLUtil.ignoreSsl()
            }

            FileUtils.copyURLToFile(httpUrl, File(dir, fileName))
        }

        fun getNetInputStream(urlStr: String): InputStream {

            val url = URL(urlStr)

            if ("https".equals(url.protocol, ignoreCase = true)) {
                SSLUtil.ignoreSsl()
            }

            val conn = url.openConnection() as HttpURLConnection
            // 设置超时间为5秒
            conn.connectTimeout = 5 * 1000
            // 防止屏蔽程序抓取而出现403错误
            // 防止屏蔽程序抓取而出现403错误
            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)")
            // 得到输入流
            // 得到输入流
            val inputStream = conn.inputStream

            return inputStream
        }

    }

}